class TradesPersonController {
    def tradeService
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ tradesPersonList: TradesPerson.list( params ) ]
    }

    def show = {
        def tradesPerson = TradesPerson.get( params.id )

        if(!tradesPerson) {
            flash.message = "TradesPerson not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ tradesPerson : tradesPerson ] }
    }

    def delete = {
        def tradesPerson = TradesPerson.get( params.id )
        if(tradesPerson) {
            tradesPerson.delete()
            flash.message = "TradesPerson ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "TradesPerson not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def tradesPerson = TradesPerson.get( params.id )

        if(!tradesPerson) {
            flash.message = "TradesPerson not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ tradesPerson : tradesPerson ]
        }
    }

    def update = {
        def tradesPerson = TradesPerson.get( params.id )
        if(tradesPerson) {
            tradesPerson.properties = params
            if(!tradesPerson.hasErrors() && tradesPerson.save()) {
                flash.message = "TradesPerson ${params.id} updated"
                redirect(action:show,id:tradesPerson.id)
            }
            else {
                render(view:'edit',model:[tradesPerson:tradesPerson])
            }
        }
        else {
            flash.message = "TradesPerson not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def tradesPerson = new TradesPerson()
        tradesPerson.properties = params
        return ['tradesPerson':tradesPerson]
    }

    def save = {
        def trades = tradeService.findOrCreateTrades(params.remove('tradesStr'))
        def tradesPerson = new TradesPerson(params)
        tradesPerson.trades = trades 
        if(!tradesPerson.hasErrors() && tradesPerson.save()) {
            flash.message = "TradesPerson ${tradesPerson.id} created"
            redirect(action:'show',id:tradesPerson.id)
        }
        else {
            render(view:'create',model:[tradesPerson:tradesPerson])
        }
    }
}
